package es.unex.meigas.gvsig.gui;

import java.awt.geom.Rectangle2D;

import com.iver.cit.gvsig.fmap.DriverException;
import com.iver.cit.gvsig.fmap.layers.FBitSet;
import com.iver.cit.gvsig.fmap.layers.FLayer;
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;

import es.unex.meigas.dataObjects.IFeature;
import es.unex.meigas.dataObjects.IVectorLayer;
import es.unex.meigas.extGIS.AbstractGISConnection;

public class gvGISConnection extends AbstractGISConnection {

	@Override
	protected void select(IVectorLayer layer, int iField) {

		FLyrVect flayer = (FLyrVect) layer.getBaseDataObject();
		SelectableDataSource recordset;
		try {
			recordset = flayer.getRecordset();
			FBitSet fbitset = recordset.getSelection();
			fbitset.set(iField);
			recordset.setSelection(fbitset);
		} catch (DriverException e) {
			e.printStackTrace();
		}

	}

	@Override
	protected void setScale(IVectorLayer layer, int minScale) {

		FLayer flayer = (FLayer) layer.getBaseDataObject();
		flayer.getMapContext().setScaleView(minScale);

	}

	@Override
	protected void zoomTo(IVectorLayer layer , IFeature feature) {

		Geometry geom = feature.getGeometry();
		Envelope env = geom.getEnvelopeInternal();
		Rectangle2D rect = new Rectangle2D.Double(env.getMinX(), env.getMinY(), env.getWidth(), env.getHeight());
		FLayer flayer = (FLayer) layer.getBaseDataObject();
		flayer.getMapContext().getViewPort().setExtent(rect);

	}

	@Override
	protected void clearSelection(IVectorLayer layer) {

		FLyrVect flayer = (FLyrVect) layer.getBaseDataObject();
		SelectableDataSource recordset;
		try {
			recordset = flayer.getRecordset();
			FBitSet fbitset = recordset.getSelection();
			fbitset.clear();
			recordset.setSelection(fbitset);
		} catch (DriverException e) {
			e.printStackTrace();
		}

	}

}
